<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hyperf_mall
 * @contact  hyperf_mall@163.com
 */

namespace App\JsonRpc;

use App\Constants\Goods;
use App\Exception\BaseException;
use App\JsonRpc\Interfaces\CateServiceInterface;
use App\Repository\GoodsCategoryRepository;
use Hyperf\RpcServer\Annotation\RpcService;

/**
 * @RpcService(name="Goods::CateService", protocol="jsonrpc", server="jsonrpc")
 */
class CateService extends BaseService implements CateServiceInterface
{

    public function init()
    {
        $this->repository = GoodsCategoryRepository::getInstance();
    }

    /**
     * Notes: 添加分类
     * Author: Bruce.z
     * DateTime: 2021/12/22 20:17
     * @param array $data
     * @return \App\Model\BaseModel|\Hyperf\Database\Model\Model
     * @throws BaseException
     */
    public function add(array $data)
    {
        $one = $this->repository->getOne(['name'=>$data['name']]);
        if(count($one)){
            throw new BaseException(BaseException::BASE_ERROR, '分类名称已存在');
        }
        return $this->repository->addData($data);
    }

    /**
     * Notes: 返回选择多级树
     * Author: Bruce.z
     * DateTime: 2021/12/29 10:52
     * @return array
     */
    public function selectTree() :array
    {
        return $this->repository->makeSelectTree(Goods::CATE_TWO);
    }

    /**
     * Notes: 获取分类树
     * Author: Bruce.z
     * DateTime: 2021/12/29 11:37
     * @return array
     */
    public function tree() :array
    {
        return $this->repository->getTree(Goods::CATE_THREE);
    }

    /**
     * Notes: 获取分类下的子分类字符串
     * Author: Bruce.z
     * DateTime: 2022/9/6 11:50
     * @param int $id
     * @return mixed
     */
    public function getCategoryIds(int $id)
    {
        return $this->repository->getCategoryIds($id);
    }
}
